﻿using System;
using System.Web;
using System.Web.Caching;

namespace doufu.Common
{
    #region Caches
    /// <summary>
    /// 缓存
    /// </summary>
    public class Caches
    {
        #region Write
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="Key">键名</param>
        /// <param name="CacheValue">值</param>
        public static void Add(string Key, object CacheValue)
        {
            if (HttpRuntime.Cache[Key] == null)
            {
                HttpRuntime.Cache.Add(Key, CacheValue, null, DateTime.Now.AddHours(10), TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
            }
            else
            {
                HttpRuntime.Cache.Insert(Key, CacheValue, null, DateTime.Now.AddHours(10), TimeSpan.Zero);
            }
        }
        #endregion

        #region Read
        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="Key">键名</param>
        /// <returns></returns>
        public static T Read<T>(string Key) where T : class
        {
            return HttpRuntime.Cache[Key] as T;
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="Key">键名</param>
        /// <returns></returns>
        public static void Delete(string Key)
        {
            if (HttpRuntime.Cache[Key] != null)
            {
                HttpRuntime.Cache.Remove(Key);
            }
        }
        #endregion
    }
    #endregion
}